﻿@page "/Base_Manage/BuildCode/List"

@inherits BaseList<DbTableInfo>

<Space Size="@("small")">
    <SpaceItem>
        <Button Type="@ButtonType.Primary" Icon="reload" OnClick="()=>Refresh()">刷新</Button>
    </SpaceItem>
    <SpaceItem>
        <Button Type="@ButtonType.Primary" Icon="plus" OnClick="()=>Edit()" Disabled=@NoneSelectedItems>生成代码</Button>
    </SpaceItem>
    <SpaceItem>
        <Select TItem="Base_DbLinkDTO"
                TItemValue="string"
                @bind-Value="LinkId"
                DataSource="Base_DbLink"
                Placeholder="关键字"
                LabelName="@nameof(Base_DbLinkDTO.LinkName)"
                ValueName="@nameof(Base_DbLinkDTO.Id)"
                OnSelectedItemChanged="OnSelectedItemChanged">
        </Select>
    </SpaceItem>
</Space>

<Table @ref="_table"
       TItem="DbTableInfo"
       DataSource="Data"
       EnableVirtualization
       Loading="Loading"
       HidePagination
       PageSize="int.MaxValue"
       @bind-SelectedRows="SelectedItems"
       ScrollX="400"
       ScrollBarWidth="12px"
       Size="TableSize.Small"
       RowClassName="@(x => x.RowIndex % 2 == 0 ?"gray-2":"")">
    <ChildContent>
        <Selection Key="@(context.TableName)" />
        <AntDesign.Column Title="表名" DataIndex="TableName" TData="string" />
        <AntDesign.Column Title="描述" DataIndex="Description" TData="string" />
    </ChildContent>
</Table>

<style>
    .gray-2 {
        background-color: #fafafa;
    }
</style>

@code
{
    public List()
    {
        Area = "Base_Manage";

    }

    private List<Base_DbLinkDTO> Base_DbLink { get; set; }
    private string LinkId { get; set; }

    protected override async Task OnInitializedAsync()
    {
        using (var waitfor = WaitFor.GetWaitFor(this))
        {
            await GetDbTableInfo();
            await base.OnInitializedAsync();
        }
    }

    private async Task GetDbTableInfo()
    {
        try
        {
            var result = await DataProvider.PostData<List<Base_DbLinkDTO>>($"/{Area}/BuildCode/GetAllDbLink");
            if (!result.Success)
            {
                throw new MsgException(result.Msg);
            }
            else
            {
                Base_DbLink = new List<Base_DbLinkDTO>(result.Data);
                LinkId = Base_DbLink.FirstOrDefault()?.Id;
            }
        }
        catch (Exception ex)
        {
            await Error.ProcessError(ex);
        }
    }

    private async void OnSelectedItemChanged(Base_DbLinkDTO value)
    {
        await GetData();
    }

    protected override async Task GetData()
    {
        using (var waitfor = WaitFor.GetWaitFor(this))
        {
            try
            {
                var result = await DataProvider.PostData<List<DbTableInfo>>($"/{Area}/BuildCode/GetDbTableList", (new { linkId = LinkId }).ToJson());
                if (!result.Success)
                {
                    throw new MsgException(result.Msg);
                }
                else
                {
                    Pagination.Total = result.Total;
                    Data = new List<DbTableInfo>(result.Data);
                }
            }
            catch (Exception ex)
            {
                await Error.ProcessError(ex);
            }
        }
    }

    protected async void Edit()
    {
        var modalConfig = new ModalOptions();
        modalConfig.Title = "代码生成";
        //modalConfig.Style = "top:20px;";
        // In order for Dispose in ConfirmTemplateDemo to take effect every time it is closed
        //modalConfig.BodyStyle += "overflow-y: auto;";
        modalConfig.Width = 600;
        modalConfig.DestroyOnClose = true;
        modalConfig.Centered = true;

        var modalRef = await ModalService.CreateModalAsync<EditForm, BuildInputDTO>(modalConfig, new BuildInputDTO() { linkId = LinkId, tables = SelectedItems.Select(p => p.TableName).ToList() });

        modalRef.OnOk = () =>
        {
            return Task.CompletedTask;
        };
    }


}